package com.itmk.student_course.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.student.entity.Student;
import com.itmk.student_course.entity.StudentCourse;
import com.itmk.student_course.service.StudentCourseService;
import com.itmk.student_course.service.impl.StudentCourseServiceImpl;
import com.itmk.student_select.entity.StudentSelectParm;
import com.itmk.student_select.entity.StudentSelectVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/studentCourse.do")
public class StudentCourseServlet extends HttpServlet {
    private StudentCourseService studentCourseService = new StudentCourseServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(method.equals("selectSave")){
            selectSave(req, resp);
        }else if(method.equals("list")){
            list(req,resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String  courseName = req.getParameter("courseName");
        //获取学生id
        HttpSession session = req.getSession();
        Student user = (Student) session.getAttribute("user");
        //封装参数
        StudentSelectParm parm = new StudentSelectParm();
        parm.setPage(page);
        parm.setLimit(limit);
        parm.setStuId(user.getStuId());
        parm.setCourseName(courseName);
        //调用service
        List<StudentSelectVo> list = studentCourseService.list(parm);
        //查询总条数
        int count = studentCourseService.getCount(parm);
        //封装返回值
        JSONObject object = new JSONObject();
        object.put("code",0);
        object.put("data",list);
        object.put("msg","查询成功");
        object.put("count",count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void selectSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String courseId = req.getParameter("courseId");
        String teacherId = req.getParameter("teacherId");
        //从session里面获取session
        HttpSession session = req.getSession();
        Student user = (Student) session.getAttribute("user");
        //封装参数
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setCourseId(courseId);
        studentCourse.setStuId(user.getStuId());
        studentCourse.setTeacherId(teacherId);
        //查询学生是否已经选课
        StudentCourse hasSelect = studentCourseService.hasSelectCourse(studentCourse);
        //判断是否选课
        if(hasSelect != null){
            //封装返回参数
            JSONObject object = new JSONObject();
            object.put("code",800);
            object.put("msg","您已经选课，不用重复选课!");
            //写回前端
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.print(object.toJSONString());
        }else{
            //选课
            studentCourseService.add(studentCourse);
            //封装返回参数
            JSONObject object = new JSONObject();
            object.put("code",200);
            object.put("msg","选课成功!");
            //写回前端
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.print(object.toJSONString());
        }
    }
}
